import Api from 'Api/index';
import appConfig from 'AppConfig/index';
import * as common from "Commons/index";
import dayjs from "dayjs";
import globalBehavior from 'Behaviors/global';

Page({
    behaviors: [globalBehavior],
    data: {
        todayDayList:[],
        todayDayList2:[{
          day: "一组固定的数据",
          list2:[{
            create_at: "2023-11-30 10:11:33",
            day: "2023-11-30",
            period: 2023138,
            list:[
              {type: 1, num: "05"},
              {type: 1, num: "12"},
              {type: 1, num: "19"},
              {type: 1, num: "28"},
              {type: 1, num: "31"},
              {type: 2, num: "06"},
              {type: 2, num: "11"},
            ]
          },{
            create_at: "2023-11-30 10:11:33",
            day: "2023-11-30",
            period: 2023138,
            list:[
              {type: 1, num: "04"},
              {type: 1, num: "06"},
              {type: 1, num: "16"},
              {type: 1, num: "28"},
              {type: 1, num: "31"},
              {type: 2, num: "04"},
              {type: 2, num: "06"},
            ]
          }
          ]
        }],
        code:'',
        priceNum:0,
        consumeNum:0,
        consume_count:0,
		win_count:0,
        historyList:[],
		

		myBuyCountInfo:{},
		
		show:false
    },
    async onLoad (options) {
        wx.setNavigationBarTitle({
            title: '创建记录'
        })
        this.getCaipiaoList()
        this.getlotteryRecordsMyBuyCount()
	},
    // 购买的情况 
    getlotteryRecordsMyBuyCount(){
        Api.caipiaoBuyCount().then(res=>{
            if( res.success ){
                res = res.result||{};
                this.setData({
                    myBuyCountInfo:res
                })
            }else{
                wx.showModal({
                    content: res.msg,
                    showCancel:false
                })
            }
        })
    },
    async getCaipiaoList(){
        Api.caipiaoList().then(res=>{
            if( res.success ){
                res = res.result||{};
                let historyList = (res.list||[]).map(item=>{
					item.create_time = dayjs(item.create_time).format("YYYY-MM-DD HH:mm:ss");
                    if( item.winning_numbers ){
                        item.winning_numbers = item.winning_numbers.split(",")
                    }else{
                        item.winning_numbers = [];
					}
					// item.lotto_numbers1 = this.setFormattingWin(this.data.lotto_numbers1,item.winning_numbers);
					// item.lotto_numbers2 = this.setFormattingWin(this.data.lotto_numbers2,item.winning_numbers);
                    item.lotto_numbers = this.setFormattingWin(item.lotto_numbers,item.winning_numbers)

                    // 开奖了  没有更新过
                    if( item.winning_numbers&&item.winning_numbers.length&&item.is_winnings_updata!=1 ){
						item.winnings = this.updataWinPrize(item.lotto_numbers,item.winning_numbers);
                        Api.caipiaoWincodePrize({ id:item._id,winnings:item.winnings });
                    }
                    return item;
                });
                // 更新中奖
                this.setData({
                    historyList:historyList
                })
            }else{
                wx.showModal({
                    content: res.msg,
                    showCancel:false
                })
            }
        });
    },
    setFormattingWin(lotto_numbers,winning_numbers=[]){
        let list = (lotto_numbers).split(",");
        return list.map(item=>{
            return {
                num:item,
                win:winning_numbers.some(e=>e==item)
            }
        })
    },
    /**
     * 更新 金额
     */
    updataWinPrize(_lotto_numbers,winning_numbers){
		let lotto_numbers = _lotto_numbers.map(item=>item.num)

        let ll_1 = lotto_numbers.slice(0,5);
        let ll_2 = lotto_numbers.slice(5);

        let ww_1 = winning_numbers.slice(0,5);
        let ww_2 = winning_numbers.slice(5);

        let oneNum = this.countCommonElements(ll_1,ww_1);
		let towNum = this.countCommonElements(ll_2,ww_2);

        var obj = {};
        obj.price = 0
        if(oneNum==5&&towNum==2){
            obj.price = 900000000
        }else if(oneNum==5&&towNum==1){
            obj.price = 800000000
        }else if(oneNum==5&&towNum==0){
            obj.price = 10000
        }else if(oneNum==4&&towNum==2){
            obj.price = 3000
        }else if(oneNum==4&&towNum==1){
            obj.price = 300
        }else if(oneNum==4&&towNum==0){
            obj.price = 100
        }else if(oneNum==3&&towNum==2){
            obj.price = 200
        }else if(oneNum==3&&towNum==1||oneNum==2&&towNum==2){
            obj.price = 15
        }else if(oneNum==3&&towNum==0||oneNum==2&&towNum==1||oneNum==1&&towNum==2||oneNum==0&&towNum==2){
            obj.price = 5
        }else{
            obj.price = 0
        }
        return obj.price||0;
    },
    // 计算两个数组有多少相同的
    countCommonElements(arr1, arr2) {  
        // 将第一个数组转换为Set，以便快速查找  
        const set1 = new Set(arr1);  
        let count = 0;  
      
        // 遍历第二个数组，检查每个元素是否在set1中  
        for (let num of arr2) {  
            if (set1.has(num)) {  
                count++;  
            }  
        }  
      
        return count;  
    },
    
    onShow: function () {

    },
	lookClick(env){
		let { day } = env.currentTarget.dataset;
		let s = (dayjs(dayjs(day).format("YYYYMMDD"))).valueOf();
		let e = (dayjs(dayjs(day).format("YYYYMMDD")+"23:59:59")).valueOf();

		let list = this.data.historyList.filter(item=>{
			return item.day>=s&&item.day<=e
		}).map(item=>{
			return item.lotto_numbers
		});
		// list.push(
		// 	this.setFormattingWin(this.data.lotto_numbers1),
		// 	this.setFormattingWin(this.data.lotto_numbers2)
		// );
		
		this.setData({
			currentList:list,
			show:true
		})
	},
    closePop(){
		this.setData({
			show:false
		})
    }
})